#ifndef __HTTP_TOOLS_H__
#define __HTTP_TOOLS_H__

#include "um_basic_types.h"
#include "umapi_os_list.h"
#include "umapi_socket.h"
#include "umapi_libc.h"
#include <stdarg.h> 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>


typedef struct
{
	UM_CHAR *key;
	UM_CHAR *value;
	struct umapi_list_head h;
}KV;

typedef struct
{
	UM_CHAR method[10];
	UM_CHAR url[100];
	UM_CHAR version[20];
	UM_S32 contentLen;
	struct umapi_list_head h;
	UM_U8 *body;		
}HttpHeaders;

typedef struct
{
	UM_VOID *p;
	UM_CHAR host[20];
	UM_S16 port;
	UM_S16 method;
	UM_CHAR path[256];
	UM_CHAR body[2048];
}S_HttpMsg;

#define MEDIA_SERVER_IP "183.56.203.38"

UM_VOID decBufferToHeaders(UM_U8 *buff, HttpHeaders *h);
UM_VOID freeHttpHeaders(HttpHeaders *h);
UM_S32 readIntByKey(UM_U8 *key, HttpHeaders *h);
UM_CHAR* readStringByKey(UM_U8 *key, HttpHeaders *h, UM_CHAR *value);
UM_CHAR * readStringByUrl(UM_CHAR *url, UM_CHAR *key, UM_CHAR *value);
UM_CHAR *getHttpParam(HttpHeaders *h, UM_CHAR *key, UM_CHAR *value);
UM_S32 readHttpPack(UM_HANDLE fd, HttpHeaders *h);
UM_VOID httpMsgInit();
UM_S32 sendNoFound(UM_HANDLE fd,UM_CHAR *serverName);
UM_S32 sendChunkHead(UM_HANDLE fd, UM_CHAR *serverName);
UM_S32 sendChunkData(UM_HANDLE fd, UM_U8 *buff,UM_S32 bufflen);
UM_S32 decUrl(UM_CHAR * host, UM_S16 * port, UM_CHAR * path,UM_CHAR* url);
static UM_S32 inline sendReply(UM_HANDLE fd, UM_CHAR *fmt, ...)
{
	UM_CHAR cmdbuff[4 * 1024];
	UM_S32 ret = 0;	
	va_list args;
	va_start(args,fmt);
	ret = vsprintf(cmdbuff, fmt, args);
	va_end(args);
	return umapi_socket_send(fd, cmdbuff, umapi_strlen(cmdbuff), 0) >0;
}

#if 1
#define MAXBUFFSIZE 13160
#define HTTPDATAOFFSET 6
#define HTTPBLOCKDATALEN 8
#else
#define MAXBUFFSIZE 1316
#define HTTPDATAOFFSET 5
#define HTTPBLOCKDATALEN 7
#endif

#define MAXTCCACHESIZE ((MAXBUFFSIZE + HTTPBLOCKDATALEN) * 2)
#define USE_HASH 1
#define USEBATCHWRITE 1

#endif
